require('../common/sidebar/index');
require('../common/header/index');

//引入account_service
var _account_service = require('service/account-service.js');
//引入工具类
const _util = require('util/util.js');
//用户注册
const Vue = require("../common/vue");

let _vm = new Vue({
    el: "#mainBody",
    data(){
        return {
            username: "",
            role: "",
            password: "",
            phone: "",
            pictureid: "",
            repeated_pwd: "",
            sex: "",
            age: "",
            email: "",
            country: "",
            signature: "",
            available: 1,
            pictureid: 6,
            suc: true
        }
    },
    methods:{
        update: function(){
            let _this = this;
            this.suc = true;
            this.check();
            if(!this.suc) return ;
            let content = {
                username: _this.username,
                password: _this.password,
                phone: _this.phone,
                pictureid: _this.pictureid,
                sex: _this.sex,
                age: _this.age,
                email: _this.email,
                country: _this.country,
                signature: _this.signature,
                available: _this.available
            };
            console.log(content);
            _account_service.updateUserinfo(content, function(res) {
                window.location.href = "/view/catalog-main.html";
            }, function(err) {
                console.log(err);
            });
                        
        },
        fetchProfile: function(_this) {
            _account_service.fetchUserinfo(function(res) {
                _this.username = res.username;
                _this.password = res.password;
                _this.phone = res.phone;
                _this.pictureid = res.pictureid;
                _this.sex = res.sex; 
                _this.age = res.age; 
                _this.email = res.email; 
                _this.country = res.country; 
                _this.signature = res.signature;
            }, function(err) {
                console.log(err);
            });
        },
        check: function() {
            if(this.password !== this.repeated_pwd){
                alert("两次填写的密码不同");
                this.suc = false;
                return ;
            }
        }
    },
    created: function() {
        this.fetchProfile(this);
    }
});
